﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using EQT.Bll;
using EQT.Model;
using EQT.Dal;
using System.Collections.Generic;

/// <summary>
/// UploadCoTestItemXls 的摘要说明
/// </summary>
public class UploadCoTestItemXls : IUploadFile
{
    DaCoTestItem dacti = DevFuture.Common.WorkContext.AppCtx.GetObject("DaCoTestItem") as DaCoTestItem;
    DaTestRecord datr = DevFuture.Common.WorkContext.AppCtx.GetObject("DaTestRecord") as DaTestRecord;
    public UploadCoTestItemXls()
    {
        //
        // TODO: 在此处添加构造函数逻辑
        //
    }

    #region IUploadFile 成员

    public void InitUpload(IUploadPage up, System.Collections.Specialized.NameValueCollection nvc)
    {
        #region 为合同检验项上传XLS原始记录


        string co_item_id =nvc["co_item_id"];
        if (String.IsNullOrEmpty(co_item_id))
        {
            Utility.SiteError("缺失所必需的参数【合同检验项编号】", up.Response);
            return;
        }
        else
        {
            up.SetConfigValue("CO_ITEM_ID", co_item_id);
            MoCoTestItem mo = new MoCoTestItem();
            mo.CoItemId = co_item_id;
            if (!dacti.GetEntityEx(ref mo))
            {
                Utility.GlobalLog.ErrorFormat("给定的合同检验项{0}无效!", co_item_id);
                Utility.SiteError("给定的合同检验项无效:" + co_item_id);
                return;
            }
            else
            {
                up.SetPageTitle( String.Format("为合同{0}的检验项{1}{2}上传原始记录文件(Excel)",
                    mo.ReportId, mo.CoItemId, mo.ItemTitle));
            }
        }
        #endregion
    }

    public int Upload1(IUploadPage up, MoTfiles file, string opter_id)
    {
        //Note:为合同检验项上传原始记录时，实际并不知道他使用的是那个原始记录模板，因此，我们要求原始记录模板可以自描述(2009-5-11)
        //Note:假定Excel文件的sheet2的单元格B1记录着此文件的模板编码；这个编码在Job分析模板时，默认的写在了这个B1单元格，用户不可更改；
        MoTestRecord mo = new MoTestRecord();
        mo.TrId = SequenceKeys.GenerateTestRecordID();//主键
        mo.CoItemId = up.GetConfigValue("CO_ITEM_ID").ToString();
        mo.TrUrl = file.FileId;//原始记录文件       

        try
        {
            return datr.AddTestRecordFile(file, mo);
        }
        catch (DalException de)
        {
            Utility.GlobalLog.Error("添加原始记录文件失败!", de);
            Utility.SiteError("添加原始记录文件失败!", up.Response);
            return -1;
        }
    }

    public int UploadedMuliti(IUploadPage up, List<MoTfiles> files, string opter_id)
    {
        throw new Exception("The method or operation is not implemented.");
    }

    public string GetReturnUrl(IUploadPage up)
    {
        throw new Exception("The method or operation is not implemented.");
    }

    #endregion
}
